﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BankCreditPortfolio.DAL;
using BankCreditPortfolio.DAL.Entities;
using BankCreditPortfolio.DAL.Services;

namespace BankCreditPortfolio.Services
{
    /// <summary>
    /// Summary description for InitializeDataService
    /// </summary>
    [WebService(Namespace = "BankCreditPortfolio")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class InitializeDataService : System.Web.Services.WebService
    {

        [WebMethod]
        public string InitializeData()
        {
            var roleService = new RoleService();
            var committeeMemberRole = new Role() { Title = "CreditCommitteeMember" };
            var adminRole = new Role() { Title = "Admin" };
            var clientRole = new Role() { Title = "Client" };
            var securityRole = new Role() { Title = "CustomerEngineer" };
            var operatorRole = new Role() { Title = "SafetyOfficer" };
            var bankerRole = new Role() { Title = "OperationsAnalyst" };
            roleService.Save(ref adminRole);
            roleService.Save(ref committeeMemberRole);
            roleService.Save(ref clientRole);
            roleService.Save(ref bankerRole);
            roleService.Save(ref securityRole);
            roleService.Save(ref operatorRole);
            var depositTypesService = new DepositTypeService();
            var dep1 = new DepositType() { Title = "Bank" };
            var dep2 = new DepositType() { Title = "Client" };
            depositTypesService.Save(ref dep1);
            depositTypesService.Save(ref dep2);
            var documentTypeService = new DocumentTypeService();
            var doc1 = new DocumentType() { Title = "Proof of income" };
            var doc2 = new DocumentType() { Title = "Information about family members" };
            documentTypeService.Save(ref doc1);
            documentTypeService.Save(ref doc2);
            var orderStatusService = new OrderStatusService();
            var orderStatus1 = new OrderStatus() { Status = "Received for review" };
            var orderStatus2 = new OrderStatus() { Status = "Correctness approved" };
            var orderStatus3 = new OrderStatus() { Status = "Safety approved" };
            var orderStatus4 = new OrderStatus() { Status = "Validity approved" };
            var orderStatus5 = new OrderStatus() { Status = "Approved" };
            var orderStatus6 = new OrderStatus() { Status = "Rejected" };
            orderStatusService.Save(ref orderStatus1);
            orderStatusService.Save(ref orderStatus2);
            orderStatusService.Save(ref orderStatus3);
            orderStatusService.Save(ref orderStatus4);
            orderStatusService.Save(ref orderStatus5);
            orderStatusService.Save(ref orderStatus6);
            var paymentStatusService = new PaymentStatusService();
            var paymentStatus1 = new PaymentStatus() { Title = "Waiting" };
            var paymentStatus2 = new PaymentStatus() { Title = "Received" };
            paymentStatusService.Save(ref paymentStatus1);
            paymentStatusService.Save(ref paymentStatus2);
            var accountService = new AccountService();
            var passwordHash = PasswordHelper.Encrypt("12345678", PasswordMode.MD5);
            var admin = new Account()
                            {
                                Email = "admin@admin.ru",
                                Role = adminRole,
                                Hash = passwordHash.Hash,
                                Salt = passwordHash.Salt
                            };
            accountService.Save(ref admin);
            var currencyService = new CurrencyService();
            var cur1 = new Currency() { Title = "BYR" };
            var cur2 = new Currency() { Title = "RUR" };
            var cur3 = new Currency() { Title = "USD" };
            var cur4 = new Currency() { Title = "EURO" };
            currencyService.Save(ref cur1);
            currencyService.Save(ref cur2);
            currencyService.Save(ref cur3);
            currencyService.Save(ref cur4);

            //var bankers = (new BankerService()).GetAll();
            //var orderHistory = new OrderHistory();
            //orderHistory.CommentsCreditCommittieMember = "1";
            //orderHistory.CommentsCustomerEngineer = "2";
            //orderHistory.CommentsOperationsAnalyst = "3";
            //orderHistory.CommentsSafetyOfficer = "4";
            //orderHistory.DateCreditCommittieMember = DateTime.Now;
            //orderHistory.DateCustomerEngineer = DateTime.Now;
            //orderHistory.DateOperationsAnalyst = DateTime.Now;
            //orderHistory.DateSafetyOfficer = DateTime.Now;
            //orderHistory.CreditCommittieMember =
            //    bankers.Where(x => x.Account.Role.Title == "CreditCommitteeMember").First();
            //orderHistory.CustomerEngineer = bankers.Where(x => x.Account.Role.Title == "CustomerEngineer").First();
            //orderHistory.OperationsAnalyst = bankers.Where(x => x.Account.Role.Title == "OperationsAnalyst").First();
            //orderHistory.SafetyOfficer = bankers.Where(x => x.Account.Role.Title == "SafetyOfficer").First();
            //orderHistory.OrderInfo = (new OrderInfoService()).GetAll().First();
            //var orderHistoryService = new OrderHistoryService();
            //orderHistoryService.Save(ref orderHistory);
            //var id = orderHistory.OrderHistoryId;
            return "Initializing is done...";
        }
    }
}
